/*
 * Yekcomputer (r) CMS
 * Copyright (c) 2009, Yekcomputer. All rights reserved.
 * Author : Babak Taraghi 'babak.taraghi@gmail.com'
 * Code licensed under the BSD License.
 * version: 1.0
 */
var mainTimerId = false;
var sessionValue = false;
var sessionCookieName = "chemialab_ci_session";

var shakeInfoAndShowContent = function(){
	$("#content").show("blind",1000);
};

var updateTime = function(){
	if(!$.cookie(sessionCookieName)){
		if(!$.cookie("remember")){
			alert("Session expired! You need to relogin to access the control panel.");
			window.location.reload();
			clearInterval(mainTimerId);
			return;
		}else{
			$.cookie(sessionCookieName,sessionValue,{path:'/'});
		}
	}else{
		sessionValue = $.cookie(sessionCookieName);
	}
	var timeContainer = document.getElementById("timeContainer");
	var now = timeContainer.innerHTML;
	splitedNow = now.split(":");
	if(parseInt(splitedNow[2]) + 1  > 59){
		splitedNow[2] = "00";
		if(!one.contentAnimating){
			$("#userInfo").show("shake",{},800);
		}
		if(parseInt(splitedNow[1]) + 1 > 59){
			splitedNow[1] = "00";
			splitedNow[0]++;
			if(parseInt(splitedNow[0]) < 10) splitedNow[0] = "0"+splitedNow[0];
		}else{
			splitedNow[1]++;
			if(parseInt(splitedNow[1]) < 10) splitedNow[1] = "0"+splitedNow[1];
		}
	}else{
		splitedNow[2]++;
		if(parseInt(splitedNow[2]) < 10) splitedNow[2] = "0"+splitedNow[2];
	}
	timeContainer.innerHTML = splitedNow[0]+":"+splitedNow[1]+":"+splitedNow[2];
};
$(document).ready(function(){
	$("#menu").fadeIn(5000);
	if(activeMenu != ""){
		$("#"+activeMenu).show("slow");
	}
	$("#headerContainer").show(400,shakeInfoAndShowContent);
	mainTimerId = setInterval(updateTime,1000);
});
var changeListOrder = function(module, sortBy, sortType) {
	var cookieName = 'listsorder';
	var pageReload = true;
	var newModule = true;
	oldCookieValue = $.cookie(cookieName);
	newCookieValue = "";
	if (oldCookieValue != null) {
		oldCookieValueArray = oldCookieValue.split(";");
		for (index in oldCookieValueArray) {
			if (oldCookieValueArray[index] != "") {
				order = oldCookieValueArray[index].split("-");
				if (order[0] == module) {
					if (order[1] == sortBy && order[2] == sortType) {
						pageReload = false;
					} else {
						order[1] = sortBy;
						order[2] = sortType;
					}
					newModule = false;
				}
				newCookieValue += order[0] + "-" + order[1] + "-" + order[2] + ";";
			}
		}
		if (newModule) {
			newCookieValue += module + "-" + sortBy + "-" + sortType + ";";
		}
	} else {
		newCookieValue = module + "-" + sortBy + "-" + sortType + ";";
	}
	$.cookie(cookieName, newCookieValue, {
		expires : 7,
		path : '/'
	});
	if (pageReload && one.lastPostURL)
		one.changeContent(one.lastPostURL);
	else if (pageReload)
		one.changeContent(window.location.href);
};
var setFormLangauge = function(lang,controllerName,modelName,languageRelation){
	one.changeContent(BASEURL+"cp/"+controllerName+"/formForLang/"+modelName+"/"+lang+"/"+languageRelation);
};

